//
//  CommonEngine.m
//  quanminzhekou
//
//  Created by zxl on 15/1/19.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "CommonEngine.h"

@interface CommonEngine ()
@property (nonatomic) MKNetworkEngine* engine;
@end

@implementation CommonEngine

+ (instancetype)sharedInstance {
    static CommonEngine* s_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_instance = [[self alloc] init];
    });
    
    return s_instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _engine = [[MKNetworkEngine alloc] initWithHostName:HostNameNetWork];
        [_engine useCache];
    }
    
    return self;
}

#pragma mark - httpMethod: get
- (MKNetworkOperation*)getOperationWithURLPath:(NSString*)path
                            shouldCache:(BOOL)isCache
                             completionHandler:(CompletionBlock)completionBlock
                                  errorHandler:(ErrorBlock)errorBlock {
    MKNetworkOperation *op = [self.engine operationWithPath:path];
    NSLog(@"url:%@", op.url);
    
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation){
        NSDictionary *result = completedOperation.responseJSON;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            if ([result isKindOfClass:[NSDictionary class]]) {
                if (completionBlock) {
                    completionBlock(result);
                }
            } else {
                if (completionBlock) {
                    completionBlock(nil);
                }
            }
        });
    } errorHandler:^(MKNetworkOperation *errorOp, NSError* error) {
        errorBlock(error);
    }];
    
    op.shouldNotCacheResponse = !isCache;
    
    [self.engine enqueueOperation:op];
    
    return op;
}

- (NSString*)urlPathForDataWithDictionary:(NSDictionary*)dic {
    return [self urlPathCompositionByFunction:@"/data?" ParameterDic:dic];
}

- (NSString*)urlPathForFocus {
    return [self urlPathCompositionByFunction:@"/focus?" ParameterDic:nil];
}

- (NSString*)urlPathForVersion {
    return [self urlPathCompositionByFunction:@"/version?device=ios" ParameterDic:nil];
}

- (NSString*)urlPathForSearchWithDictionary:(NSDictionary*)dic {
    return [self urlPathCompositionByFunction:@"/search?" ParameterDic:dic];
}

- (NSString*)urlPathForUpnum {
    return [self urlPathCompositionByFunction:@"/upnum" ParameterDic:nil];
}

#pragma mark - 
- (NSString*)urlPathCompositionByFunction:(NSString*)string ParameterDic:(NSDictionary*)dic {
    
    NSMutableString* path = [[NSMutableString alloc] initWithString:string];
    
    if ([dic isKindOfClass:[NSDictionary class]] && 0 < dic.allKeys.count) {
        for (NSInteger index = 0; index < dic.allKeys.count; ++index) {
            if (0 != index) {
                [path appendString:@"&"];
            }
            
            NSString* key = [dic.allKeys objectAtIndex:index];
            NSString* value = [dic objectForKey:key];
            
            [path appendFormat:@"%@=%@", key, value];
        }
    }
    
    return path;
}

@end
